package gui.abstracts;

import gui.tables.TableGroup;
import gui.tables.TableStudent;
import gui.tables.models.MyTableModel;

import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JPanel;

import commandPatern.Delete;
import commandPatern.Insert;
import commandPatern.StoreCommands;
import commandPatern.Update;

import bean.UniversityBean;
import dao.StudentDAO;
import dao.UniversityDAO;

public abstract class AbstractPanel extends JPanel {
	protected AbstractTable jAbstractTable;
	protected Update update = null;
	protected Vector<Integer> editRowColomn = new Vector<Integer>();

	public void removeRowFromTable() throws InstantiationException,
			IllegalAccessException, ClassNotFoundException, SQLException {
		UniversityBean univBean = null;

		univBean = getUniversityBean(jAbstractTable.getSelectedRow());
		
		StoreCommands.getInstance().add(new Delete(univBean,jAbstractTable.getSelectedRow(), jAbstractTable ));
		
		jAbstractTable.getDefaultTableMOdel().removeRow(
				jAbstractTable.getSelectedRow());

		UniversityDAO uDAO = univBean.getDAO();
		uDAO.Delet(univBean);
	}

	public abstract UniversityBean getUniversityBean(int i);

	public void cancelInsert() {
		jAbstractTable.cancelInsertRow();
	}

	public void insertObjectInDb() throws InstantiationException,
			IllegalAccessException, ClassNotFoundException, SQLException {

		UniversityBean un = getUniversityBean(-1);
		UniversityDAO uDAO = un.getDAO();
		int iD = uDAO.Insert(un);

		un.setId(iD);

		StoreCommands.getInstance().add(new Insert(un, jAbstractTable));

		jAbstractTable.setIdAfterInsert(iD);
		
		System.out.println("insertObjectInDb");
		jAbstractTable.setInsertNewRow(false);
		
		
	}

	public void edit() throws InstantiationException, IllegalAccessException,
			ClassNotFoundException, SQLException {
		UniversityBean un = getUniversityBean(jAbstractTable.getSelectedRow());
		update = new Update(un, jAbstractTable.getSelectedRow(), jAbstractTable);
		jAbstractTable.setLastRow(jAbstractTable.getSelectedRow());
		jAbstractTable.setEdit(true);
		editRowColomn.add(Integer.parseInt(jAbstractTable.getValueAt(
				jAbstractTable.getSelectedRow(), 0).toString()));
		System.out.println("din edit "+editRowColomn.get(0));

	}

	public void SubmitEdit() throws InstantiationException,
			IllegalAccessException, ClassNotFoundException, SQLException {
		UniversityBean un = getUniversityBean(jAbstractTable.getSelectedRow());
		UniversityDAO uDAO = un.getDAO();
		uDAO.update(un);

		update.setUniversityNew(un);
		StoreCommands.getInstance().add(update);

		jAbstractTable.setEdit(false);
	}
	
	public abstract void cancelEdit() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException;

	public void cancelEditTotal(UniversityDAO DAO){
		System.out.println("cancelEditTotal");
		//System.out.println("cancelEditTotal " + editRowColomn.get(0));
		Vector<Integer> dateFromObject = null;
		
		UniversityDAO grDAO = DAO;
		
		dateFromObject = grDAO.getUniversityDAOById(editRowColomn.get(0));
		System.out.println("cancelEditTotal  1");
		dateFromObject.add(0, editRowColomn.get(0));

		jAbstractTable.getDefaultTableMOdel().removeRow(
				jAbstractTable.getLastRow());

		jAbstractTable.getDefaultTableMOdel().insertRow(
				jAbstractTable.getLastRow(), dateFromObject);
		jAbstractTable.setEdit(false);
	}

	public abstract AbstractTable getTable();
}
